<template>
  <div>
    <div class="page-body">
      <div class="page-body-logo">
        <!-- <img :src="LOGIN_IMG" /> -->
      </div>
      <!-- <div class="page-body-collapsebtn" @click="appAction.setMenuCollapse(!appState.menuCollapse)">
        <div class="page-left-btn" :class="{ 'page-left-btn-rotate': appState.menuCollapse }">
          <img class="page-btn-icon" src="@/assets/app/category.svg" />
        </div>
      </div> -->
      <div class="page-body-con">
        {{ APP_TITLE }}
      </div>
      <div class="page-body-right">
        <!-- <div class="page-btn">
          <el-tooltip class="item" effect="dark" content="操作手册" placement="bottom">
            <img class="page-btn-icon" src="@/assets/app/bzwd.svg" />
          </el-tooltip>
        </div>
        <div class="page-btn">
          <el-tooltip class="item" effect="dark" content="刷新" placement="bottom">
            <img class="page-btn-icon" src="@/assets/app/sx.svg" />
          </el-tooltip>
        </div>-->
        <div class="page-btn">
          <el-tooltip class="item" effect="dark" content="退出" placement="bottom" style="margin-right: 5px;">
            <img class="page-btn-icon" src="@/assets/app/out.svg" @click="logout" />
          </el-tooltip>
        </div>
        <div class="page-btn">
          <el-tooltip class="item" effect="dark" content="修改密码" placement="bottom" style="margin-left: 5px;">
            <img class="page-btn-icon" src="@/assets/app/paswrd.svg" @click="changePassword" />
          </el-tooltip>
        </div>
        <div class="page-btn page-btn-avatar" @click="onClickUserModel">
          <div class="page-avatar" :style="{ 'background-image': `url(${avatar})` }"></div>
        </div>
      </div>
    </div>
    <!-- 个人用户信息 -->
    <app-avatar ref="appAvatarRef" @model-change="onModelChange"></app-avatar>

    <password-modal v-if="isPasswordModalView" ref="passwordModalRef" @submit="" @close="isPasswordModalView = false">
    </password-modal>
  </div>
</template>

<script lang="ts">
import { defineComponent, reactive, ref, computed, nextTick } from "vue";
import { getFileUrl } from "@/utils/bus";

export default defineComponent({
  name: "AppHeader",
})
</script>

<script lang="ts" setup>
import { appAction, appState } from "@/store/app";
import { ElMessage, ElMessageBox } from "element-plus";
import passwordModal from "@/components/password"
import AppAvatar from "./AppAvatar.vue";
import { LOGIN_IMG,APP_TITLE } from "@/config";

/**
 * 修改密码弹框
 */
let isPasswordModalView = ref(false)
let passwordModalRef = ref()
function logout() {
  ElMessageBox.confirm(
    '确认要退出登录吗?',
    '提示',
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning',
    }
  )
    .then(() => {
      ElMessage.success("退出成功")
      //置空token
      appAction.setToken("");
      //重置登录
      appAction.setIsAppCreated(false);
      //然后直接让页面刷新就可以了
      window.location.reload();
      // request("/Login/LoginOut", {}, { methods: "get" }).then(() => {
      //   ElMessage.success("退出成功")
      //   //置空token
      //   appAction.setToken("");
      //   //重置登录
      //   appAction.setIsAppCreated(false);
      //   //然后直接让页面刷新就可以了
      //   window.location.reload();
      // })
    })
}
/**
 * 打开修改密码弹框
 */
function changePassword() {
  isPasswordModalView.value = true;
  nextTick(() => {
    passwordModalRef.value.show({ ...appState.userInfo }, 1);
  })
}

/**
 * ==========================
 *  头像相关
 * ==========================
 */

/**
 * 头像背景图片
 */
let avatar = computed(() => {
  return appState.userInfo._profile || require("@/assets/app/tx.svg");
});

/**
 * 弹框
 */
let appAvatarRef = ref();

/**
 * 弹框展示
 */
let appAvatarShow: boolean = false;

/**
 * 点击打开弹框
 */
function onClickUserModel() {
  if (appAvatarRef.value && !appAvatarShow) {
    appAvatarRef.value.show();
  }
}

/**
 * 弹框是否展示
 */
function onModelChange(show: boolean) {
  appAvatarShow = show;
}
</script>

<style lang="less" scoped>
@import url("../../config.less");

.page-body {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 100%;
  padding: 0 10px 0 5px;
  // background-color: #f7f7f7;
  background-color: white;
  border-radius: var(--app-border-radius);
}

.page-body-logo {
  display: flex;
  align-items: center;
  padding-left: 20px;

  >img {
    height: 40px;
  }
}

.page-body-collapsebtn {
  flex-shrink: 0;
}

.page-left-btn {
  width: 40px;
  height: 40px;
  display: flex;
  justify-content: center;
  align-items: center;
  transition: transform .3s;
  cursor: pointer;
}

.page-left-btn-rotate {
  transform: rotate(-90deg);
}

.page-body-con {
  flex-grow: 1;
  display: flex;
  justify-content: center;
  align-items: center;
  font-weight: bold;
  font-size: 18px;

}

.page-body-right {
  display: flex;
  align-items: center;
}



.page-btn {
  margin-left: 10px;
  border-radius: 3px;
  width: 40px;
  height: 40px;
  display: flex;
  justify-content: center;
  align-items: center;
  cursor: pointer;
  flex-shrink: 0;

  &:hover {
    background-color: rgba(0, 0, 0, 0.1);
  }
}

.page-btn-icon {
  width: 20px;
  height: 20px;
}

.page-btn-avatar {
  border-radius: 100%;
  transition: background-color 0.2s;
}

.page-avatar {
  width: 32px;
  height: 32px;
  background-color: rgba(0, 0, 0, 0.1);
  border-radius: 100%;
  border: 1px solid var(--primaryColor);
  background-size: 100% 100%;
  background-position: center;
  background-repeat: no-repeat;
}


// 用户弹框
.page-user {
  position: fixed;
  right: 10px;
  width: 200px;
  height: 200px;
  border: 1px solid rgba(0, 0, 0, 0.1);
  background-color: white;
  z-index: 2;
  border-radius: 3px;
  box-shadow: 0 0 10px 1px rgba(0, 0, 0, 0.05);
}
</style>
